import { defineStore } from 'pinia'
import Cookies from "js-cookie";

export const useUserStore = defineStore('user', {
  state: () => ({
    user_info: Cookies.get('user_info') ? JSON.parse(Cookies.get('user_info')) : null
  }),
  actions: {
    setUserInfo(info) {
      this.user_info = info
      // 设置token过期时间为2小时
      const expires = new Date(new Date().getTime() + 2 * 60 * 60 * 1000)
      Cookies.set('user_info', JSON.stringify(info), { expires })
    },
    clearUserInfo() {
      this.user_info = null
      Cookies.remove('user_info')
    }
  }
})
